﻿#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace Patterns.Validation.Formatting
{
	internal class TimeSpanValueFormatter : IValueFormatter
	{
		#region IValueFormatter Members

		public bool CanHandle(object value)
		{
			return value is TimeSpan;
		}

		public string ToString(object value, int nestedPropertyLevel = 0)
		{
			var timeSpan = (TimeSpan) value;

			string[] fragments = GetNonZeroFragments(timeSpan).ToArray();

			if (!fragments.Any())
				return "default";

			string sign = timeSpan.TotalMilliseconds >= 0 ? string.Empty : "-";

			return fragments.Count() == 1 
				? sign + fragments.Single() 
				: sign + JoinUsingWritingStyle(fragments);
		}

		#endregion

		private static IEnumerable<string> GetNonZeroFragments(TimeSpan timeSpan)
		{
			TimeSpan absoluteTimespan = timeSpan.Duration();

			var fragments = new List<string>();

			AddDaysIfNotZero(absoluteTimespan, fragments);
			AddHoursIfNotZero(absoluteTimespan, fragments);
			AddMinutesIfNotZero(absoluteTimespan, fragments);
			AddSecondsIfNotZero(absoluteTimespan, fragments);

			return fragments;
		}

		private static void AddSecondsIfNotZero(TimeSpan timeSpan, List<string> fragments)
		{
			if ((timeSpan.Seconds > 0) || (timeSpan.Milliseconds > 0))
			{
				string result = timeSpan.Seconds.ToString();

				if (timeSpan.Milliseconds > 0)
					result += "." + timeSpan.Milliseconds.ToString("000");

				fragments.Add(result + "s");
			}
		}

		private static void AddMinutesIfNotZero(TimeSpan timeSpan, List<string> fragments)
		{
			if (timeSpan.Minutes > 0)
				fragments.Add(timeSpan.Minutes + "m");
		}

		private static void AddHoursIfNotZero(TimeSpan timeSpan, List<string> fragments)
		{
			if (timeSpan.Hours > 0)
				fragments.Add(timeSpan.Hours + "h");
		}

		private static void AddDaysIfNotZero(TimeSpan timeSpan, List<string> fragments)
		{
			if (timeSpan.Days > 0)
				fragments.Add(timeSpan.Days + "d");
		}

		private static string JoinUsingWritingStyle(string[] fragments)
		{
			return string.Join(", ", AllButLastFragment(fragments)) + " and " + fragments.Last();
		}

		private static string[] AllButLastFragment(ICollection<string> fragments)
		{
			return fragments.Take(fragments.Count - 1).ToArray();
		}
	}
}